package org.soarsky.wealthchengdu.google.map;


import org.soarsky.wealthchengdu.R;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;


import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;

public class LocationOperator implements LocationOverlaysInterface{
	private Activity mContext = null;
	private AbstractGoogleMap mAbstractGoogleMap=null;
	private MapOverlaysControlFactory mControlFactory = null;
	private LocationOverlaysInterface mOverlaysInterface;
	private View popupLocation = null;
	private TextView mTitleView = null;
	private TextView mContentView = null;
	private MapView mapView=null;
	public LocationOperator(Activity context,AbstractGoogleMap abstractGoogleMap){
		mContext = context;
		
		mAbstractGoogleMap = abstractGoogleMap;
		mControlFactory = mAbstractGoogleMap.getMapOverlaysControlFactory();
		mapView = mAbstractGoogleMap.getMapView();
		mOverlaysInterface = mControlFactory.getLocationOverlays();
		initView();
		initEven();
	}
	private void initView(){
		LayoutInflater inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);;
		popupLocation = inflater.inflate(R.layout.google_map_loc_popup, null);
		mContentView = (TextView) popupLocation.findViewById(R.id.mapsnippet);
		mTitleView = (TextView) popupLocation.findViewById(R.id.maptitle);
		
	}
	private void initEven(){
		mOverlaysInterface.setOnTapCallBack(new OverlayCallBack() {
			
			@Override
			public void onLostFocus() {
				// TODO Auto-generated method stub
				if (popupLocation != null && popupLocation.isShown()) {
					popupLocation.setVisibility(View.GONE);
					mapView.invalidate();
				//	mapControlData.setShowPopupDialog(false);

				}
			}
			
			@Override
			public void onGotFocus(BasePoint basePoint) {
				// TODO Auto-generated method stub
				
					showPopupLocation(
							(LocationPoint)basePoint,getGeoPoint());
				
			
			}
		});
	}
	@Override
	public void locationChange(LocationPoint location) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void locationChange(LocationPoint location, boolean isInvalidate) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void animateTo() {
		// TODO Auto-generated method stub
		mOverlaysInterface.animateTo();
		
	}
	@Override
	public void animateTo(int x, int y) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void setCenter() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void setCenter(int x, int y) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void setOnFocus(boolean foucs) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public boolean getOnFocus() {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean disableLocation() {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean enableLocation() {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public void setLcationBearing(float bearing) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void setOnTapCallBack(OverlayCallBack callBack) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void freeLocation() {
		// TODO Auto-generated method stub
		
	}
	
	private void showPopupLocation(LocationPoint loc,GeoPoint geoPoint) {
		animateTo();
		if (loc == null) {
			
			return;
		}
		
		if (popupLocation.getVisibility() != View.VISIBLE)
			popupLocation.setVisibility(View.VISIBLE);
		
		mTitleView.setText(mContext.getResources().getString(R.string.map_current_location));
		//if(loc.getAddress()!=null&&!loc.getAddress().equals(""))
		//	mContentView.setText(loc.getAddress());
		mapView.removeAllViews();
		mapView.addView(popupLocation, new MapView.LayoutParams(
				MapView.LayoutParams.WRAP_CONTENT,
				MapView.LayoutParams.WRAP_CONTENT, geoPoint, 0, -8,
				MapView.LayoutParams.BOTTOM_CENTER));
	
	}
	@Override
	public void createGeoPoint(LocationPoint location) {
		// TODO Auto-generated method stub
		mOverlaysInterface.createGeoPoint(location);
	}
	@Override
	public GeoPoint getGeoPoint() {
		// TODO Auto-generated method stub
		return mOverlaysInterface.getGeoPoint();
	}
	 
}
